package com.itheima.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.service.ClazzService;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RequestMapping("/clazzs")
@RestController
public class ClazzController {

    //注入service层对象
    @Autowired
    private ClazzService clazzService;


    @GetMapping
    public Result list(String name,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
                       @RequestParam(defaultValue = "1") int page,
                       @RequestParam(defaultValue = "10") int pageSize) {
        log.info("查询班级列表");

        // 调用Service层查询分页数据
        PageResult<Clazz> pageResult = clazzService.getPageResult(name,begin,end,page,pageSize);

        return Result.success(pageResult);
    }

    @PostMapping
    public Result save(@RequestBody Clazz clazz) {
        log.info("新增班级：{}", clazz);

        //调用方法，添加班级，因为前端接口对班主任的选项用的是"/list" 接口，
        //所以在EmpContronller文件中调用empService.list()方法即可实现班主任回显
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        clazzService.save(clazz);

        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("根据id查询班级：{}", id);
        Clazz clazz = clazzService.getById(id);
        return Result.success(clazz);
    }

    @PutMapping
    public Result update(@RequestBody Clazz clazz) {
        log.info("修改班级：{}", clazz);
        clazz.setUpdateTime(LocalDateTime.now());
        clazzService.updateById(clazz);

        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("删除班级：{}", id);

        try {
            clazzService.deleteById(id);
            return Result.success();
        } catch (SecurityException e) {

            //接收service层自定义抛出的异常
            return Result.error(e.getMessage());
        } catch (Exception e) {

            //处理其他异常
            return Result.error("你的网络有误，请检查网络");
        }

    }

    /**
     * 查询全部班级
     * @return
     */
    @GetMapping("/list")
    public Result list() {
        log.info("查询全部班级");

        //查询全部班级
        List<Clazz> clazzList = clazzService.list();
        return Result.success(clazzList);
    }
}
